var e = require;
var t = module;
var o = exports;
var n;
Object.defineProperty(o, "__esModule", {value: !0}), (o.BannerType = void 0);
var r,
    s = e("SDK"),
    c = e("VivoAd"),
    l = e("NativeAdComponent"),
    d = e("AdUtils"),
    u = cc._decorator,
    p = u.ccclass;
u.property,
    (function (e) {
        (e.BOTTOM_CENTER = "bottom|center"), (e.BOTTOM_LEFT = "bottom|left"), (e.BOTTOM_RIGHT = "bottom|right");
    })((r = o.BannerType || (o.BannerType = {})));
var h = (function (e) {
    function t() {
        var t = (null !== e && e.apply(this, arguments)) || this;
        return (
            (t._bannerTemplate = null),
            (t._bannerList = []),
            (t._isShow = !1),
            (t._templateBannerList = []),
            (t._bannerShow = []),
            (t.isClose = !1),
            (t._adRequestTime = 0),
            t
        );
    }
    return (
        __extends(t, e),
        (t.prototype.init = function (e) {
            (this._config = e),
                (this._isBanner = e.banner_ad_enbale > 0),
                (this._bannerCode = e.banner_code),
                this._info || (this._info = qg.getSystemInfoSync());
        }),
        (t.prototype.initCustomTemplate = function (e) {
            var t = this;
            void 0 === e && (e = r.BOTTOM_CENTER),
                d.default.instance.log("创建最新的banner广告"),
                console.log("----------------创建最新的banner广告---------------", e);
            var o = this._info,
                n = this;
            d.default.instance.log("创建最新的banner广告: " + o.screenHeight + "  " + (o.screenHeight - 525)),
                (this._bannerTemplate = (function () {
                    var o = {posId: t._bannerCode, style: {gravity: e}},
                        i = n._config.custom_banner_request;
                    if (i && "" != i) for (var a in (i = JSON.parse(i))) o[a] = i[a];
                    var r = o.style;
                    return (
                        s.default.Instance.isPortrait() && (r.width = 720),
                        r.top && (r.top = s.default.Instance.getSystemInfoSync().screenHeight - 750),
                        console.log("custom ad request: ", JSON.stringify(o)),
                        qg.createCustomAd(o)
                    );
                })()),
                (this.isClose = !1),
                this._bannerTemplate.onLoad(function () {
                    d.default.instance.log("原生模板广告加载完成"),
                        console.log("开始展示原生模板广告。。。。。", t.isClose),
                        t.isClose || d.default.instance.delayShow(i, t._config);
                }),
                this._bannerTemplate.onError(function (e) {
                    d.default.instance.log("原生模板广告加载失败" + JSON.stringify(e));
                }),
                this._bannerTemplate.onClose(function () {
                    d.default.instance.log("原生模板广告关闭"),
                        s.default.Instance.reportLog(s.NodeAction.NATIVE_CLOSE, "原生模板广告关闭"),
                        (t._isShow = !1),
                        d.default.instance.log("原生模板广告关闭 ");
                });
            var i = function () {
                t._bannerTemplate &&
                    !t.isClose &&
                    t._bannerTemplate
                        .show()
                        .then(function () {
                            d.default.instance.log("原生模板广告展示完成"),
                                (t._isShow = !0),
                                s.default.Instance.reportLog(s.NodeAction.NATIVE_SHOW, "原生模板广告展示");
                        })
                        .catch(function () {});
            };
        }),
        (t.prototype.initBannerTemplate = function (e, t, o) {
            var n = this;
            void 0 === e && (e = !1), void 0 === t && (t = 0), void 0 === o && (o = 0);
            var i = this,
                a = qg.getSystemInfoSync();
            d.default.instance.log(a);
            var r =
                a.screenWidth > a.screenHeight
                    ? a.screenWidth <= 720
                        ? a.screenWidth / 2
                        : a.screenWidth / 2 - 30
                    : a.screenWidth;
            d.default.instance.log("创建banner广告: " + this._bannerCode),
                d.default.instance.log("bannertargetBannerAdWidth : " + r);
            var c = a.screenHeight - 170,
                l = (a.screenWidth - r) / 2;
            e && (c = 0),
                (c += t),
                (l += o),
                (this._bannerTemplate = qg.createBannerAd({
                    posId: i._bannerCode,
                    style: {top: c, left: l},
                    adIntervals: 30
                })),
                this._bannerTemplate.onSize(function (e) {
                    d.default.instance.log("banner 宽度：" + e.width + ", banner 高度：" + e.height);
                }),
                this._bannerTemplate.onLoad(function () {
                    d.default.instance.log("banner 广告加载成功");
                }),
                this._bannerTemplate.onError(function (e) {
                    d.default.instance.log("banner 广告加载错误：" + JSON.stringify(e));
                }),
                this._bannerTemplate.onClose(function () {
                    d.default.instance.log("banner 广告关闭："),
                        i.hideBannerTemplate(),
                        s.default.Instance.reportLog(s.NodeAction.BANNER_CLOSE, "模板Banner关闭");
                }),
                this._bannerShow.push(this._bannerTemplate),
                this._bannerTemplate
                    .show()
                    .then(function () {
                        console.log("banner广告展示完成"),
                            (n._isShow = !0),
                            s.default.Instance.reportLog(s.NodeAction.BANNER_SHOW, "模板Banner展示"),
                            n._showCb && (console.log("banner广告展示完成_showCb"), n._showCb());
                    })
                    .catch(function (e) {
                        d.default.instance.log("banner广告展示失败" + JSON.stringify(e)), n.hideBanner();
                    });
        }),
        (t.prototype.showBannerTemplate = function (e, t, o, n) {
            var i = this;
            if (
                (void 0 === e && (e = r.BOTTOM_CENTER),
                void 0 === t && (t = !1),
                void 0 === o && (o = 0),
                void 0 === n && (n = 0),
                1 == this._config.banner_ad_enbale)
            ) {
                var a = new Date().getTime();
                if (
                    !(0 != this._adRequestTime && a - this._adRequestTime <= 1e3) &&
                    ((this._adRequestTime = a), this._isBanner)
                ) {
                    this.hideBanner();
                    var s = this._config.inser_ad_delayed_show / 2;
                    this.scheduleOnce(function () {
                        console.log("---------showBannerTemplate-------创建最新的banner广告---------------"),
                            i.initCustomTemplate(e);
                    }, s);
                }
            }
        }),
        (t.prototype.hideBannerTemplate = function () {
            this._bannerShow.forEach(function (e) {
                e && (e.hide(), e.destroy(), (e = null));
            }),
                (this._bannerShow = []),
                this._bannerTemplate &&
                    ((this._isShow = !1),
                    this._bannerTemplate.hide(),
                    this._bannerTemplate.destroy(),
                    (this._bannerTemplate = null));
        }),
        (t.prototype.showBanner = function (e, t, o, n, i, a) {
            if (
                (void 0 === e && (e = r.BOTTOM_CENTER),
                void 0 === t && (t = l.NativeType.BANNER),
                void 0 === o && (o = !1),
                void 0 === n && (n = 0),
                void 0 === i && (i = 0),
                void 0 === a && (a = null),
                (this._bannerType = t),
                d.default.instance.log("***********showNativeBanner type :" + t),
                console.log("banner广告展示完成showCb: " + this._isBanner),
                this._isBanner ? (this._showCb = a) : a && a(),
                t != l.NativeType.BANNER_NO_NATIVE)
            ) {
                var u = this;
                if (null != this._config && 1 == this._config.enable && 1 == this._config.banner_ad_enbale) {
                    var p = c.default.Instance.getVivoNativeData();
                    if (null != p && p.isReady()) {
                        var h = function () {
                                c.default.Instance.creatorNativeData(),
                                    s.default.Instance.reportLog(s.NodeAction.NATIVE_CLICK, "原生Banner点击");
                            },
                            f = function () {
                                c.default.Instance.creatorNativeData(),
                                    s.default.Instance.reportLog(s.NodeAction.NATIVE_SHOW, "原生Banner展示");
                            },
                            _ = function () {
                                u.cleanBanner(),
                                    s.default.Instance.reportLog(s.NodeAction.NATIVE_CLOSE, "原生Banner关闭");
                            },
                            y = cc.director.getScene().children[0],
                            m = y.getChildByName("banner");
                        m && (m.removeFromParent(), m.destroy());
                        var g = cc.view.getVisibleSize(),
                            v = 0,
                            w = 0;
                        (v = o ? g.height / 2 - 75 : -g.height / 2 + 75),
                            d.default.instance.log("-------bannerType: " + this._bannerType);
                        var b = "sdk/res/prefabs/NativeBannerAd";
                        t == l.NativeType.BANNER_BUTTON
                            ? ((b = "sdk/res/prefabs/NativeBannerAddButtonAd"),
                              (v = o ? g.height / 2 - 200 : -g.height / 2 + 200))
                            : t == l.NativeType.BANNER_LITTLE_BUTTON &&
                              ((v = o ? g.height / 2 - 150 : -g.height / 2 + 150),
                              (b = "sdk/res/prefabs/NativeBannerLittleBUttonAd")),
                            0 != i && (v = i),
                            0 != n && (w = n),
                            d.default.instance.loadPrefabs(b, function (e) {
                                var o = cc.instantiate(e);
                                (u._nativeBanner = o.addComponent(l.default)),
                                    u._nativeBanner.initData(p),
                                    u._nativeBanner.listionCall(f, h, _),
                                    (o.width = g.width),
                                    u._nativeBanner.showAd(t, u._config, w, v),
                                    y.addChild(o, 1e3, "banner"),
                                    u.cacheBanner(o);
                            });
                    } else d.default.instance.log("************showNativeBanner 广告数据不存在");
                }
            } else this.showBannerTemplate(e, o, n, i);
        }),
        (t.prototype.cacheBanner = function (e) {
            this.cleanBanner(), this._bannerList && this._bannerList.push(e);
        }),
        (t.prototype.cleanBanner = function () {
            this._bannerList &&
                this._bannerList.length > 0 &&
                (this._bannerList.forEach(function (e) {
                    e.removeFromParent(), e.destroy();
                }),
                (this._bannerList = []));
        }),
        (t.prototype.hideBanner = function (e) {
            void 0 === e && (e = !0),
                d.default.instance.log("------hideNativeBanner: " + e),
                this._bannerType != l.NativeType.BANNER_NO_NATIVE
                    ? (d.default.instance.log("------hideNativeBanner2: " + e),
                      e && (this._bannerType = null),
                      this._nativeBanner && (this._nativeBanner.hide(!0), (this._nativeBanner = null)))
                    : this.hideBannerTemplate();
        }),
        __decorate([p], t)
    );
})(cc.Component);
o.default = h;
